﻿// This dialog checks to see if the user is sure they want to add a candidate
// that appears to already be in the database

var CHECK_DLG_WIDTH = 400;
var CHECK_DLG_HEIGHT = 300;

var CheckCandidateDlg_Name = "";
var CheckCandidateDlg_Phone = "";
var CheckCandidateDlg_Email = "";
var CheckCandidateDlg_HeardAboutID = -1;
var CheckCandidateDlg_HeardAboutNotes = "";

// Check to see if the Check Candidate popup came from the "Add Application" button
var CheckCandidateDlg_IsAddingApp = false;

//this function runs on document.ready()
$(function () {
    //create an add candidate popup dlg
    $("#CheckCandidatePopup").dialog({
        modal: true,
        resizable: false,
        width: CHECK_DLG_WIDTH,
        height: CHECK_DLG_HEIGHT,
        position: ['center', 'center'],
        autoOpen: false,
        buttons:
        [
            {
                text: "Yes",
                click: function () { $("#AddCandidatePopup").dialog("close"); OnConfirmAddCandClicked(); }
            },
            {
                text: "No",
                click: function () { $("#AddCandidatePopup").dialog("close"); $(this).dialog("close"); }
            }
        ],
        title: "Confirm",
        open: OnCheckCandidateDlgOpen
    });

    $("#CheckCandidateList").menu({});

});

function OnCheckCandidateDlgOpen() 
{
    $("#CheckCandidatePopup_Confirm").html("");
    $("#CheckCandidatePopup_Confirm").append("Are you sure you want to add " + CheckCandidateDlg_Name + "?");

    var candidate_Json;
    var candidateID_Json;
    var phone_Json;
    var email_Json;

    PageMethods.CheckCandidates_Json(CheckCandidateDlg_Name,
        function (json) {
            candidate_Json = (eval(json));

            $("#CheckCandidateList").html("");
            for (var i in candidate_Json) {
                var candName = candidate_Json[i].value;
                var candEmail = candidate_Json[i].email;
                var candPhone = candidate_Json[i].phone;
                var candInfo = candName + " " + candEmail + " " + candPhone;
                var startTag = '<li class="ui-menu-item" role="menuitem"><a href="#" class="ui-corner-all" tabindex="-1">';
                //var startTag = '<li>';
                var endTag = '</a></li>';
                //var endTag = '</li>';
                var completeInfo = startTag + candInfo + endTag;

                $("#CheckCandidateList").append(completeInfo);
            }

        },
        function () {
            alert("ERROR");
        }
    );
}

function OnConfirmAddCandClicked() {
    $("#CheckCandidatePopup_Error").addClass("ui-helper-hidden");

        //call page methods to add candidate
    PageMethods.CreateCandidate(CheckCandidateDlg_Name, CheckCandidateDlg_Email, CheckCandidateDlg_Phone, CheckCandidateDlg_HeardAboutID, CheckCandidateDlg_HeardAboutNotes,
            function (newID) {
                //TODO Show success msgg???

                if (CheckCandidateDlg_IsAddingApp) {
                    ApplicationDlg_CurrentCandidateID = newID;
                    ShowPopup("#AddApplicationPopup"); 
                }

                //close the dlg
                $("#AddCandidatePopup").dialog("close");
                $("#CheckCandidatePopup").dialog("close");
            },
            function () {
                //error, show message and don't close dlg
                $("#CheckCandidatePopup_Error").removeClass("ui-helper-hidden");
            }
        );   // end: PageMethods.CreateCandidate
}


